//
//  HEGoodsListViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/31.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEGoodsListViewController.h"
#import "ZHFilterMenuView.h"
#import "HEGoodsMenuView.h"
#import "HEBrandModel.h"
#import "HEGoodsCell.h"
#import "HEGoodsDetailsViewController.h"

@interface HEGoodsListViewController ()<ZHFilterMenuViewDelegate,ZHFilterMenuViewDetaSource, HEGoodsMenuViewDelegate, UITableViewDelegate, UITableViewDataSource>


@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) ZHFilterMenuView *menuView;
@property (nonatomic, strong) HEGoodsMenuView *menu;

@property (nonatomic, strong) NSMutableArray <HEBrandModel *>*brandArr;

@property (nonatomic, strong) NSString *brandIds;
@property (nonatomic, strong) NSString *minPrice;
@property (nonatomic, strong) NSString *maxPrice;
@property (nonatomic, assign) NSInteger orderBy;
@property (nonatomic, assign) NSInteger sort;

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;
 
@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HEGoodsModel *> * dataSourceArr;


@end

@implementation HEGoodsListViewController

- (NSMutableArray<HEBrandModel *> *)brandArr {
    if (!_brandArr) {
        _brandArr = [NSMutableArray array];
    }
    return _brandArr;
}

- (NSMutableArray<HEGoodsModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"商品列表";
    
    _pageNum = 1;
    _pageSize = 20;
    _orderBy = 1;
    
    
    [self initUI];
    
    [self loadData];
    [self loadMenuData];
    
    [self setCustomTableView:_tableView];
}

- (ZHFilterMenuView *)menuView
{
    if (!_menuView) {
        _menuView = [[ZHFilterMenuView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, CGRectGetWidth(self.view.frame), 45) maxHeight:CGRectGetHeight(self.view.frame) - 45 - ZXNavBarHeight];
        _menuView.zh_delegate = self;
        _menuView.zh_dataSource = self;
        
        _menuView.titleArr = @[@"销量",@"价格",@"筛选"];
        _menuView.imageNameArr = @[@"desc",@"desc",@"selection_normal"];

    
        [self.view addSubview:_menuView];
    }
    return _menuView;
}

#pragma mark -

- (void)loadData {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[NSNumber numberWithInteger:_categId] forKey:@"productCategoryId"];
    [dict setValue:_brandIds forKey:@"brandId"];
    [dict setValue:_minPrice forKey:@"minPrice"];
    [dict setValue:_maxPrice forKey:@"maxPrice"];
    [dict setValue:[NSNumber numberWithInteger:_orderBy] forKey:@"orderBy"];
    [dict setValue:[NSNumber numberWithInteger:_sort] forKey:@"sort"];
    [dict setValue:[NSNumber numberWithInteger:_pageNum] forKey:@"pageNum"];
    [dict setValue:[NSNumber numberWithInteger:_pageSize] forKey:@"pageSize"];

    [WOAFNetWorkingHandle GETWithBaseUrl:KServerUrl suffixUrl:KGoodsList parameters:dict success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        
        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }
        
        HEMallPageResponse *response = [HEMallPageResponse mj_objectWithKeyValues:responseObject];
        [self.dataSourceArr addObjectsFromArray:[HEGoodsModel mj_objectArrayWithKeyValuesArray:response.data.records]];
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }else {
            if (response.data.records.count == 0) {
                [Dialog toast:@"没有更多数据了~" delay:2.0];
            }
        }

    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)loadMenuData {
    [WOAFNetWorkingHandle GETWithBaseUrl:KServerUrl suffixUrl:KgetListByCategId parameters:@{@"id":[NSNumber numberWithInteger:_categId]} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);

        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        [self.brandArr addObjectsFromArray:[HEBrandModel mj_objectArrayWithKeyValuesArray:response.data]];

        self.menu.brandArr = self.brandArr;
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)initUI {
    
    _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight + 50, KScreenWidth, KScreenHeight - ZXNavBarHeight - 50) style:UITableViewStylePlain];
//    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.separatorColor = KHexColor(@"EEEEEE");

    _tableView.scrollsToTop = YES;
    _tableView.tableFooterView = [[UIView alloc] init];
    
    self.tableView.backgroundColor = KWhiteColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    __weak __typeof(&*self)weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        weakSelf.isFromHeadRefresh = YES;
        weakSelf.pageNum = 1;
        [weakSelf loadData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        weakSelf.isFromHeadRefresh = NO;
        weakSelf.pageNum += 1;
        [weakSelf loadData];
    }];
    
    // MJRefreshAutoNormalFooter
//    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
//        weakSelf.isFromHeadRefresh = NO;
//        weakSelf.pageNum += 20;
//        [weakSelf loadData];
//    }];
    
    [_tableView registerClass:[HEGoodsCell class] forCellReuseIdentifier:KCellGoodId];

    
    [self.view addSubview:self.menu];
}

- (HEGoodsMenuView *)menu {
    if (!_menu) {
        _menu = [[HEGoodsMenuView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, 50) maxHeight:(KScreenHeight - ZXNavBarHeight - 50)];
        _menu.delegate = self;
    }
    return _menu;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:KCellGoodId];
    cell.model = _dataSourceArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: KCellGoodId cacheByIndexPath:indexPath configuration:^(HEGoodsCell *cell) {
        cell.model = self.dataSourceArr[indexPath.row];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEGoodsModel *model = _dataSourceArr[indexPath.row];
    HEGoodsDetailsViewController *vc = [HEGoodsDetailsViewController new];
    vc.goodsId = model.id;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - HEGoodsMenuViewDelegate
- (void)menuView:(HEGoodsMenuView *)view didSelectSoltAtIndex:(NSInteger)index isDesc:(BOOL)isDesc {
    if (index == 0) {
        _sort = 1;
    }else {
        _sort = 2;
    }
    
    _orderBy = isDesc ? 1 : 2;
    
    [self.tableView.mj_header beginRefreshing];
}


- (void)menuView:(HEGoodsMenuView *)menuView didSelectConfirmWithBradIds:(NSString *)brandis minPrice:(NSString *)minPrice maxPrice:(NSString *)maxPrice {
    
    _brandIds = brandis;
    _minPrice = minPrice;
    _maxPrice = maxPrice;
    
    [self.tableView.mj_header beginRefreshing];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
